package com.xiaochangwei.web.controller;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.ModelAttribute;

import com.xiaochangwei.web.beans.User;
import com.xiaochangwei.web.utils.Constant;

/**
 * @since 2017年3月9日 下午3:56:37
 * @author 肖昌伟 317409898@qq.com
 * @description
 */
public class BaseController {
	private static final ThreadLocal<HttpSession> sessionLocal = new ThreadLocal<HttpSession>();

	@ModelAttribute
	public void setHttpSession(HttpSession session) {
		sessionLocal.remove();
		sessionLocal.set(session);
	}

	public User getCurrentUser() {
		return (User) sessionLocal.get().getAttribute(Constant.SESSION);
	}

	public Long getUserId() {
		if (getCurrentUser() != null) {
			return getCurrentUser().getId();
		}
		return null;
	}

}
